package com.aitaoxue.palx.ui;

import uk.co.senab.photoview.PhotoView;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.InjectView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.api.ApiHttpClient;
import com.aitaoxue.palx.base.BaseActivity;
import com.aitaoxue.palx.widget.HackyViewPager;
import com.nostra13.universalimageloader.core.ImageLoader;

public class PhotosActivity extends BaseActivity implements
		OnPageChangeListener {

	@InjectView(R.id.view_pager)
	HackyViewPager viewPager;
	@InjectView(R.id.error_gallery_header)
	RelativeLayout head;
	@InjectView(R.id.error_gallery_tail)
	LinearLayout tail;
	@InjectView(R.id.errorbook_gallery_go_back)
	ImageView goBack;
	@InjectView(R.id.errorbook_gallery_head_tv)
	TextView titleTv;
	@InjectView(R.id.errorbook_gallery_head_index)
	TextView indexTv;
	@InjectView(R.id.errorbook_galllery_desc)
	TextView desc;

	private float lastX;
	private float lastY;
	private boolean visible = true;

	// 索引
	private int index;
	// 总数量
	private int count;
	private String[] times;
	private String[] descs;
	private String[] images;

	@Override
	protected int getLayoutId() {
		return R.layout.multi_photos;
	}

	@Override
	protected boolean hasActionBar() {
		return false;
	}

	@Override
	public void onClick(View v) {
		switch (view.getId()) {
		case R.id.errorbook_gallery_go_back:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public void initView() {
		count = getIntent().getIntExtra("count", 0);
		index = getIntent().getIntExtra("index", 0);

		times = getIntent().getStringArrayExtra("times");
		descs = getIntent().getStringArrayExtra("descs");
		images = getIntent().getStringArrayExtra("imgs");

		viewPager.setAdapter(new ViewPagerAdapter(images));
		viewPager.setOnPageChangeListener(this);
		
		viewPager.setCurrentItem(index);
		titleTv.setText(times[index]);
		desc.setText(descs[index]);
		indexTv.setText(String.valueOf(index+1)+"/"+String.valueOf(count));
	}

	@Override
	public void initData() {
		// 初始化数据

	}

	static class ViewPagerAdapter extends PagerAdapter {

		private String[] images;

		public ViewPagerAdapter(String[] images) {
			this.images = images;
		}

		@Override
		public int getCount() {
			return images.length;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(container.getContext());

			ImageLoader.getInstance().displayImage(
					ApiHttpClient.ERROR_BOOK_PREFIX + images[position],
					photoView);

			container.addView(photoView, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);

			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			lastX = ev.getX();
			lastY = ev.getY();

			break;
		case MotionEvent.ACTION_UP:
			float currentX = ev.getX();
			float currentY = ev.getY();

			if (currentX == lastX && currentY == lastY) {
				// Toast.makeText(ViewPagerActivity.this, "µãÁËÎÒÅ¶",
				// Toast.LENGTH_SHORT).show();

				if (visible) {
					head.setVisibility(View.GONE);
					tail.setVisibility(View.GONE);
				} else {
					head.setVisibility(View.VISIBLE);
					tail.setVisibility(View.VISIBLE);
				}

				visible = !visible;

			}

			break;
		default:
			break;
		}

		return super.dispatchTouchEvent(ev);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int position) {
		titleTv.setText(times[position]);
		desc.setText(descs[position]);
		indexTv.setText(String.valueOf(position+1) + "/" + String.valueOf(count));
	}

}
